package com.baosight.sczx.zm.cb.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cb.Coalseam;
import com.baosight.sczx.common.zm.cInterface.cb.RoadWay;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cb.domain.ZMCBLevel;
import com.baosight.sczx.zm.cb.domain.ZMCBMiningarea;

/**
 * <p>Title: ServiceZMCB02.java</p>
 * <p>Description: 水平信息</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2013-07-03下午15:31:29  高强  Create
 *</pre>
 */
public class ServiceZMCB02 extends ServiceEPBase{
	private String fdMineId ="";
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2013-7-2上午11:03:09  高强
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
//		EiInfo outInfo = this.query(inInfo);
		inInfo.addBlock(RoadWay.getYesORNoBlock("cmes.cb.IsFirstH"));//是否为第一水平
		
		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.roadwayType", false));// 查询区'巷道类型'
		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.mineRock", false));// 煤岩别
		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.supportingType", true));// 支护形式
		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.roadwayUsing", false));// 巷道用途
		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.grubProcess", false));// 掘进工艺
//		inInfo.set("roadwayNoType", SystemSettingCS.getInstance().getRoadwayNo());// 巷道编号形式 0自动 1手动 2生成
		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.sectionShape", true));// 断面形状
		inInfo.addBlock(RoadWay.getParnetInfoRoadWayBlock());// 巷道所属下拉框
		inInfo.addBlock(Coalseam.getWorkStatusBlock("cmes.cb.status"));// 施工状态
		return inInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2013-7-2上午11:03:09  高强
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo)
	{
		fdMineId = inInfo.getString("fdMineId");
		String[] _nodeId=fdMineId.split("_");
		fdMineId=_nodeId[0];
		StaticTools.setQueryParameters(inInfo, "fdMineId", fdMineId, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCBLevel.query", new ZMCBLevel());
		outInfo.addBlock(RoadWay.getYesORNoBlock("cmes.cb.IsFirstH"));//是否为第一水平
		return outInfo;
	}
	
	
	
	/**
	 * 新增水平信息
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2013-7-2上午11:03:09  高强
	 * </p>
	 *
	 */
	public EiInfo insert(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCBLevel _dom = new ZMCBLevel();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			if(_dom.getFdIsFirst().equals("是"))//保证只有一个第一水平
			{
				CheckFirstH(fdMineId);
			}
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CI_LEVEL"));//主键
			_dom.setFdMineId(fdMineId);//所属矿井 
			this.dao.insert("ZMCBLevel.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * 修改水平信息
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2013-7-2上午11:03:09  高强
	 * </p>
	 *
	 */
	public EiInfo update(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCBLevel _dom = new ZMCBLevel();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			if(_dom.getFdIsFirst().equals("是"))//保证只有一个第一水平
			{
				CheckFirstH(fdMineId);
			}
			_dom.setFdMineId(fdMineId);//所属矿井 
			this.dao.update("ZMCBLevel.update",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功"); 
		return out;
	}
	
	/**
	 * 删除水平信息
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2013-7-2上午11:03:09  高强
	 * </p>
	 *
	 */
	public EiInfo delete(EiInfo inInfo) 
	{
		for(int i=0;i<inInfo.getBlock(EiConstant.resultBlock).getRowCount();i++)
		{
			String fdKeyId = inInfo.getBlock("result").getCellStr(i, "fdKeyId").toString();
			boolean validate = this.canDelete(inInfo, fdKeyId);//水平是否被引用
			if(validate) {
				return inInfo;
			}
		}
		EiInfo _temp = super.delete(inInfo, "ZMCBLevel.delete");//删除水平记录
		EiInfo outInfo = this.query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
		
	}
	
	/**
	 * 校验名称和编号
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2013-7-2上午11:03:09  高强
	 *   2016-7-10 20:35:24 lhx modify
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		Map<String, String> params = new HashMap<String, String>();
		ZMCBLevel _dom = new ZMCBLevel();
		EiBlock eiBlock = inInfo.getBlock("result");
		String oper=(String)inInfo.get("efCurButtonEname");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String fdNo = _dom.getFdNo();
			String fdName = _dom.getFdName();
			String MineId=_dom.getFdMineId();
			if(oper.equalsIgnoreCase("update")){
				params.put("fdKeyId",_dom.getFdKeyId());
			}
			if(MineId==null ||MineId.trim().equals("")){
				MineId=fdMineId;
			}
			params.put("fdMineId", MineId);
			params.put("fdNo", fdNo);
			params.put("fdName", fdName);
			List result = dao.query("ZMCBLevel.validate", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCBLevel _temp = (ZMCBLevel)result.get(0);
				
				StringBuffer msg = new StringBuffer("提交失败，");
				if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)){
					msg.append(" 水平编号 ");
				}
				if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
					msg.append(" 水平名称 ");
				}
				msg.append("重复！");
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
	
	/**
	 * 检查水平是否可以删除
	 * <p>Function: canDelete</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-7-4下午01:32:59 高强
	 * </p>
	 *
	 */
	private boolean canDelete(EiInfo inInfo, String fdKeyId) {
		if(fdKeyId == null) {
			fdKeyId = new String("");
		}
		
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdKeyId", fdKeyId);
		
		List<?> _result = dao.query("ZMCBMiningarea.canDelete", _argMap);
		
		if(_result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("有采区已经关联了此水平，不能删除！");
			return true;
			
		}
        List<?> _result1 = dao.query("ZMCBRoadway.canDelete", _argMap);
		
		if(_result1.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("有巷道已经关联了此水平，不能删除！");
			return true;
			
		}
		return false;
	}
	
	/**
	 * 保证第一水平只有一条数据
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2013-7-2上午11:03:09  高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public void CheckFirstH(String mainId)
	{
		Map _args = new HashMap();
		_args.put("fdMineId", mainId);
		//_args.put("fdKeyId", keyId);
		_args.put("fdIsFirst", "否");
		dao.update("ZMCBLevel.updateFirst", _args);
	}
}
